var attendanceType;
var id;
var groupid;
var iconid;
var subid;
var menuid;
var businessId;
$(function(){
   	 groupid = ly.getUrlParam("groupid");
	 iconid = ly.getUrlParam("iconid");
	 subid = ly.getUrlParam("subid");
	 menuid = ly.getUrlParam("menuid");	

	attendanceType = ly.getUrlParam("attendanceType");
	id = ly.getUrlParam("id");
	if(attendanceType == 2){
		$(".body_title").html("编辑");
		  $(".form").getForm({
	        id: id,
	        ajaxform:true,
	   },function (data) {
	   		selectbusinessList(data.equipmentId);
			$("#isUsing").getSelect({
				type:"radio",
				asName:"isUsing",
				list:[
					{value:"1",display:"启用"},
					{value:"0",display:"禁用"}
				],
				default:data.isUsing			
			});
			$("#checkShifts").getSelect({
				type:"radio",
				asName:"checkShifts",
				code:"checkShifts",
				default:data.checkShifts		
			});
	    });
	}else{
		selectbusinessList("");
		$("#isUsing").getSelect({
			type:"radio",
			asName:"isUsing",
			list:[
				{value:"1",display:"启用"},
				{value:"0",display:"禁用"}
			],
			default:'1'			
		});
		$("#checkShifts").getSelect({
			type:"radio",
			asName:"checkShifts",
			code:"checkShifts",
			default:1		
		});
	}
	
	//获取到选择的值
	$("body").on("click","input[name='userType']",function(){
		var userType = this.value;
		if(userType == '2'){
			$("#changeBusinessId").removeClass("hide");
			selectbusinessList(businessId);
		}else{
			$("#changeBusinessId").addClass("hide");
		}
	});
	
	//提交
    $("body").on("click","#save-btn-commodityClassification",function(event){
		event.preventDefault();
		
		var success =  $('.form').validate('submitValidate');
		var checkStartTime = $("#checkStartTime").val();
		var checkEndTime = $("#checkEndTime").val();
        if(success){
			//验证通过提交表单
			console.log(countDown(checkStartTime));
			console.log(countDown(checkEndTime));
		
			if(checkEndTime<checkStartTime){
				ly.toast('考勤结束时间应在开始时间之后');
			}else{
				submitFom();
			}
     
        }
    })
	
	//批量删除
	$("#batchDelBtn").click(function(){
		var c = "";
		$("input[name=datachk]:checked").each(function(){
			c += $(this).val()+',';
		});
		deleteFirstCategory(c);
	});
});
function countDown(time) {

	var s = 0;
	
	var hour = time.split(':')[0];
	
	var min = time.split(':')[1];
	
	var sec = time.split(':')[2];
	
	s = Number(hour * 3600) + Number(min * 60) + Number(sec);
	
	return s;
	
	};
/*
 * 提交表单
 */
function submitFom(){
    var checkName =$("#checkName").val();
 	var distance = $("#distance").val();
 	var checkStartTime = $("#checkStartTime").val();
 	var checkEndTime = $("#checkEndTime").val();
 	var equipmentId = $('select[name=equipmentId] option:checked').val();
 	var isUsing = $("#isUsing input:checked").val();
 	var checkShifts = $("#checkShifts input:checked").val();
  	var obj = {};
  	var url = site.apiUrl+"/back/attendanceBack/insertOrUpdateAttendanceSettings";
  	if(attendanceType == 1){
		//新增
		 obj = {
	    	'checkName':checkName,
	    	'distance':distance,
	    	'checkStartTime':checkStartTime,
	    	'checkEndTime':checkEndTime,
	    	'equipmentId':equipmentId,
	    	'checkShifts':checkShifts,
	    	'isUsing':isUsing
	    };
  	}else{
  		//修改
  		var thisId = $("#id").val();
  		obj = {
  			'checkName':checkName,
	    	'distance':distance,
	    	'checkStartTime':checkStartTime,
	    	'checkEndTime':checkEndTime,
	    	'equipmentId':equipmentId,
	    	'isUsing':isUsing,
	    	"checkShifts":checkShifts,
  			'id':thisId
  		};
  	}
  	console.log(obj);
	request({
		url:url,
		data:obj,
		type:"post",

		callback:function(f) {
			console.log(f);
			if(f.code=="rest.success"){
				if(attendanceType == 1){
					ly.alert('保存成功',function(){	            
			    		ly.goto("attendance-settings.html");
			        });
				}else{
					ly.alert('修改成功',function(){	           
			    		ly.goto("attendance-settings.html");
			        });
				}
			}else{
				ly.toast(f.desc);
			}
		}
	});
  	
}

function selectbusinessList(equipmentId){
	var list = new Array();
	request({
			url:site.apiUrl+"/back/attendanceBack/selectEquipmentListBack",
			data:{'num':1,'size':100,'isUsing':1,'equipmentType':1},	
			type:"get",
			ajaxform:true,
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.equipmentName};
							list.push(obj);
						});
					}
					$("#equipmentId").getSelect({
						type: "select",
						asName: "equipmentId",
						list:list,
						default:equipmentId
					});
				}
			}
		});	
}